<?php
/**
 * FetchModel.php
 * 
 * @category   Naneau
 * @package    Naneau_Action_Helper
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 * @version    0.1
 */

/** Base Class */
require_once 'Zend/Controller/Action/Helper/Abstract.php';

/**
 * Empty exception
 * @see ErrorController
 */
require_once 'Naneau/Exception/Empty.php';

/**
 * Naneau_Controller_Action_Helper_FetchModelRow
 * 
 * fetch a model row
 *
 * @category   Naneau
 * @package    Naneau_Action_Helper
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 */
class Naneau_Controller_Action_Helper_FetchModelRow extends Zend_Controller_Action_Helper_Abstract  {

    /**
     * alias for fetch()
     *
     * @see fetch()
     * @param Zend_Db_Table_Abstract $model
     * @param string $id
     * @return Zend_Db_Table_Row
     */
    public function direct() {
        $args = func_get_args();
        return call_user_func_array(array($this, 'fetch'), $args);
    }

    /**
     * fetch an instance by id
     *
     * @param Zend_Db_Table_Abstract $model
     * @param string $id
     * @return Zend_Db_Table_Row
     */
    public function fetch($model, $id) {
        $key = $this->getRequest()->getParam($id);
        //the key we're looking for

        if ($key === null) {
            //key isn't in request
            throw new Naneau_Exception_Empty('ID: ' . $id . ' is not in the request');
        }

        $rowset = $model->find($key);

        if ($rowset->count() == 0) {
            //empty rowset
            throw new Naneau_Exception_Empty('no row with key: ' . $key);
        }

        return $rowset->current();
    }
}